package com.beiyou.orderingsystemonweb.user.controller;

import com.beiyou.orderingsystemonweb.shop.domain.UserAddress;
import com.beiyou.orderingsystemonweb.shop.service.ShopService;
import com.beiyou.orderingsystemonweb.user.domain.User;
import com.beiyou.orderingsystemonweb.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * 用户主页
 *
 */
@Controller
@RequestMapping("/user")
public class UserHomeController {
    @Autowired
    ShopService shopService;
    @RequestMapping("/afterLogin.do")
    public String fistGoHome(){
        //第一次成功登录,重定向
        return "redirect:/user/userHome.do";
    }
    /**
     * 导航栏
     * 1主页
     * 2我的购物车
     */
    @RequestMapping("/userHome.do")
    public String userHome(HttpServletRequest request, HttpSession session){
        //设置导航栏信息
        setNavName(request,"index");
        //设置分页面rui
        setUriName(request,"/user/foodList.do?begin=0&size=5");

        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }

    @RequestMapping("/userCar.do")
    public String userCar(HttpServletRequest request){
        //设置导航栏信息
        setNavName(request,"userCar");
        //设置分页面rui
        setUriName(request,"/user/goUserCar.do");

        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }
    @RequestMapping("/interest.do")
    public String interest(HttpServletRequest request){
        //设置导航栏信息
        setNavName(request,"interest");
        //设置分页面rui
        setUriName(request,"/user/goInterest.do");

        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }
    @RequestMapping("/goInterest.do")
    public String goInterest(HttpServletRequest request,HttpSession session){
        String userId = (String)session.getAttribute("userId");
        //查询
        Map<String, Object> interestShopList = userService.queryInterestShopListByUid(userId);
        interestShopList.entrySet().forEach(map-> request.setAttribute(map.getKey(), map.getValue()));
        return "/user/userinterest";
    }
    @RequestMapping("/person.do")
    public String person(HttpServletRequest request){
        //设置导航栏信息
        setNavName(request,"person");
        //设置分页面rui
        setUriName(request,"/user/goPerson.do");

        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }
    @RequestMapping("/goPerson.do")
    public String goPerson(HttpServletRequest request,HttpSession session){
        String userId = (String)session.getAttribute("userId");
        request.setAttribute("fileImg",request.getServletContext().getRealPath("/statics/images/user/"));
        request.setAttribute("person",userService.queryPersonByUid(userId));
        return "/user/person";
    }
    @RequestMapping("/history.do")
    public String history(HttpServletRequest request){
        //设置导航栏信息
        setNavName(request,"history");
        //设置分页面rui
        setUriName(request,"/user/goHistory.do");

        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }
    @RequestMapping("/goHistory.do")
    public String goHistory(HttpServletRequest request,HttpSession session){
        String userId = (String)session.getAttribute("userId");
        Map<String, Object> map = userService.queryCountOnMontyByUid(userId);
        if (map!=null){
            request.setAttribute("DataList",map.get("DataList"));
            request.setAttribute("month",map.get("month"));
        }
        return "/user/data";
    }
    @RequestMapping("/ordering.do")
    public String ordering(HttpServletRequest request){
        //设置导航栏信息
        setNavName(request,"ordering");
        //设置分页面rui
        setUriName(request,"/user/goOrdering.do");

        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }
    @RequestMapping("/goOrdering.do")
    public String goOrdering(HttpServletRequest request,HttpSession session){
        String userId = (String)session.getAttribute("userId");
        Map<String, Object> map = userService.queryUserOrderInfoByUserId(userId);
        if (map==null){
            return "/user/userorder";
        }
        request.setAttribute("userName",map.get("userName"));
        request.setAttribute("userId",map.get("userId"));
        request.setAttribute("userImg",map.get("userImg"));
        request.setAttribute("orderList",map.get("orderList"));
        request.setAttribute("orderMsg",map.get("orderMsg"));
        return "/user/userorder";
    }

    public void setNavName(HttpServletRequest request,String NavName){
        request.setAttribute("navName",NavName);
    }
//    request.setAttribute("uri","/main/goPerson.do?uid="+uid); //直接写后端的请求地址相当于写get请求
    public void setUriName(HttpServletRequest request,String uri){
        request.setAttribute("uri",uri);
    }


    @RequestMapping("/allFinal.do")
    public String allFinal(HttpServletRequest request, HttpSession session){
        String userId = (String) session.getAttribute("userId");
        //检查是否有默认送餐地址
        //送餐地址最终需要放在请求域中
        //直接放对象，不单个放字段了
        if(request.getAttribute("userAddress")==null){
            //查询是否有送餐地址
            UserAddress userAddress = shopService.queryUserAddressByUid(userId);
            if (userAddress !=null){
                //左侧有这个东西说明就不需要提示框
                request.setAttribute("userAddress",userAddress);
            }
        }
        User u;
        if (request.getAttribute("userPerson")==null){
            //用户个人信息
            u = userService.queryUserById(userId);
            request.setAttribute("user",u);
        }
        if (request.getAttribute("orderNum1")==null){
            //用户送餐状态
            Map<String,Object> map = userService.queryOrderCountByUid(userId);
            Set<Map.Entry<String, Object>> set = map.entrySet();
            set.forEach((mapEntry)->{
                request.setAttribute(mapEntry.getKey(), mapEntry.getValue());
            });
        }
        if(request.getAttribute("interestList")==null){
            List<Map> interestList = userService.queryInterestByUid(userId);
            request.setAttribute("interestList",interestList);
        }

        //转发到页面
        return "/user/userhome";
    }

    //分页面
    /**
     * 分页显示店铺菜品信息
     * 显示我的购物车
     */
    @RequestMapping("/foodList.do")
    public String foodList(HttpServletRequest request, HttpSession session,Integer begin,Integer size){
        //得到用户的默认送餐地址
        String userId = (String)session.getAttribute("userId");
        //如果有默认送餐地址就不用了---由于这是新的请求，所以送餐地址不可能在请求域中
        //不放到会话域，占内存
        UserAddress userAddress = shopService.queryUserAddressByUid(userId);
        if (userAddress!=null){

        }
        else{
            request.setAttribute("cpoymsg","暂未查询到您的送餐地址，请添加送餐地址后重新刷新页面");
        }

        //通过用户的lng,lat查询
        String lng = userAddress.getLng();
        String lat = userAddress.getLat();
        //规定一页最多5条
        List list = foodList(lng, lat, begin, size);
        //第一次js不能处理
        //判断一下是否为空，然后提醒用户一下
        if(list.size()<2){
            request.setAttribute("cpoymsg","抱歉，您的附近暂未查询到店铺信息");
        }
        else{
            request.setAttribute("foodList",list);
        }
        return "/user/foodlist";
    }

    @RequestMapping("/goUserCar.do")
    public String goUserCar(HttpServletRequest request,HttpSession session){
        String userId = (String)session.getAttribute("userId");

        Map<String, Object> maps = userService.queryCarByUid(userId);
        if (maps==null){
            request.setAttribute("carNum",0);
            return "/user/usercar";
        }
        request.setAttribute("carNum",maps.get("carNum"));
        request.setAttribute("shopList",maps.get("shopList"));
        return "/user/usercar";
    }


    /**
     * ajax请求
     * @param lng:经度,字符串，数据库可以处理
     * @param lat:纬度,
     * @param begin:当前页数-1
     * @param size:每一页最多多少行
     * @return List数组：第一个字段不用，非空标志，剩下的是数据库查询结果
     */
    @RequestMapping("/foodList.data")
    @ResponseBody
    public List foodList(String lng, String lat, Integer begin, Integer size){
        if (lng==null||lat==null||begin==null||size==null){
            return null;
        }
        List list = new ArrayList();
//        list.add(new Object());
        list.add("");//添加一行
        list.addAll(shopService.queryShopByPos(lng,lat,begin,size));//追加查询结果

        return list;
    }

    @RequestMapping("/addInterset.do")
    public String addInterset(String uid,String sid){
        if (sid==null){
            return null;
        }
        //关注店铺
        Integer integer = shopService.addInterset(sid, uid);
        return "redirect:/user/userHome.do";
    }



//跳转到添加送餐地址页面
    @RequestMapping("/goAddress.do")
    public String goAddress(){
        return "/user/userAddress";
    }

//  ajax请求添加送餐信息
    @RequestMapping("/addAddress.data")
    @ResponseBody
    public Object addAddress(String uid,String uname,String uphone,String pos,String lng,String lat){
        if (uid==null||uname==null||uphone==null||pos==null||lng==null||lat==null){
            return null;
        }
        return userService.addAddress(uid,uname,uphone,pos,lng,lat);//返回是一个数值,不为0就行
    }
    @Autowired
    UserService userService;


    //左边的请求：类似导航栏，刷新页面
    @RequestMapping("/userInterest.do")
    public String userInterest(String shopId,HttpServletRequest request){
        //设置导航栏信息
        setNavName(request,"interest");
        //设置分页面rui
        setUriName(request,"/user/goUserInterest.do?sid="+shopId+"&begin=0"+"&end=15");
        //添加背景显示
        request.setAttribute("interestId",shopId);
        //转发到左侧页面
        return "forward:/user/allFinal.do";
    }
    @RequestMapping("/goUserInterest.do")
    public String goUserInterest(String sid,HttpServletRequest request,Integer begin,Integer end){
        //通过sid查询
        Map<String,Object> map = userService.userInterest(sid,begin,end);
        request.setAttribute("shopName",map.get("shopName"));
        request.setAttribute("interestNum",map.get("num"));
        request.setAttribute("foodList",map.get("foodList"));
        return "/user/usershop";
    }

    /**
     * 查询订单记录
     * 当前月份
     * 优惠信息，总价
     */
    @RequestMapping("/getData.data")
    @ResponseBody
    public Map<String,Object> getData(String uid){
        if (uid==null){
            return null;
        }
        Map<String, Object> map = userService.queryCountOnMontyByUid(uid);
        return map;
    }
}
